//
//  AppDelegate.m
//  BuffetNews
//
//  Created by 李荃荃 on 14-10-27.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "AppDelegate.h"
#import "MainViewController.h"
#import "RecommentViewController.h"
#import "ChosenViewController.h"
#import "FindViewController.h"
#import "WebColor.h"
#import <ShareSDK/ShareSDK.h>
#import "WeiboApi.h"
#import "WXApi.h"
#import "WeiboSDK.h"
#import <RennSDK/RennSDK.h>
#import <QuartzCore/QuartzCore.h>
#import "NightModel.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    //  订阅
    MainViewController *mainVC = [[MainViewController alloc] init];
    self.tabOne = [[UINavigationController alloc] initWithRootViewController:mainVC];
    self.tabOne.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"订阅" image:[UIImage imageNamed:@"iconfont-jingxuan.png"] tag:1000];
    
    
    //  推荐
    RecommentViewController *recoVC = [[RecommentViewController alloc] init];
    self.tabTow = [[UINavigationController alloc] initWithRootViewController:recoVC];
    _tabTow.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"热点" image:[UIImage imageNamed:@"iconfont-tuijian.png"] tag:2000];
    
    //  发现(精选)
    ChosenViewController *chosenVC = [[ChosenViewController alloc] init];
    self.tabThree = [[UINavigationController alloc] initWithRootViewController:chosenVC];
    self.tabThree.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"精选" image:[UIImage imageNamed:@"iconfont-redianyaowen.png"] tag:3000];
    
    //  内容库
    FindViewController *findVC = [[FindViewController alloc] init];
    self.tabFour = [[UINavigationController alloc] initWithRootViewController:findVC];
    self.tabFour.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"内容库" image:[UIImage imageNamed:@"iconfont-ziliaoku-2.png"] tag:4000];
    
    
    self.tabBar = [[UITabBarController alloc] init];
    self.tabBar.viewControllers = @[_tabOne, _tabTow, _tabThree, _tabFour];
    self.window.rootViewController = _tabBar;

    [_tabOne.navigationBar setBackgroundImage:[UIImage imageNamed:@"moren0.png"] forBarMetrics:UIBarMetricsDefault];
    [_tabTow.navigationBar setBackgroundImage:[UIImage imageNamed:@"moren0.png"] forBarMetrics:UIBarMetricsDefault];
    [_tabThree.navigationBar setBackgroundImage:[UIImage imageNamed:@"moren0.png"] forBarMetrics:UIBarMetricsDefault];
    [_tabFour.navigationBar setBackgroundImage:[UIImage imageNamed:@"moren0.png"] forBarMetrics:UIBarMetricsDefault];
    
    
    _tabBar.tabBar.backgroundImage = [UIImage imageNamed:@"yu.png"];
    _tabBar.tabBar.tintColor = [WebColor deepSkyBlue];
    
    
    _tabOne.navigationBar.translucent = NO;
    _tabTow.navigationBar.translucent = NO;
    _tabThree.navigationBar.translucent = NO;
    _tabFour.navigationBar.translucent = NO;
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(tabChangeColor:) name:@"Night" object:nil];
    [center addObserver:self selector:@selector(topicColor:) name:@"topicColor" object:nil];
    
    [ShareSDK registerApp:@"42a1ec46ef15"];     //参数为ShareSDK官网中添加应用后得到的AppKey
    //添加新浪微博应用 注册网址 http://open.weibo.com
    [ShareSDK connectSinaWeiboWithAppKey:@"2313267090"
                               appSecret:@"ce0176ece99d98125acf0c1c6ef02dc3"
                             redirectUri:@"http://www.shareSDK.com"];
    
    [ShareSDK connectTencentWeiboWithAppKey:@"42a1ec46ef15"
                                  appSecret:@"c7d68b92edc33d011d6ffffdb76baf08"
                                redirectUri:@"http://www.sharesdk.cn"
                                   wbApiCls:[WeiboApi class]];
    [ShareSDK connectWeChatWithAppId:@"wx4868b35061f87885"
                           wechatCls:[WXApi class]];
    //当使用新浪微博客户端分享的时候需要按照下面的方法来初始化新浪的平台
    [ShareSDK  connectSinaWeiboWithAppKey:@"2313267090"
                                appSecret:@"ce0176ece99d98125acf0c1c6ef02dc3"
                              redirectUri:@"http://www.shareSDK.com"
                              weiboSDKCls:[WeiboSDK class]];
    
    [ShareSDK connectRenRenWithAppId:@"273230"
                              appKey:@"fe079f1491564ccca41f57978d8677d1"
                           appSecret:@"51c1a7718a564c05ab0cfdecb20ce189"
                   renrenClientClass:[RennClient class]];

    [_tabBar release];
    [mainVC release];
    [_tabOne release];
    [recoVC release];
    [_tabTow release];
    [chosenVC release];
    [_tabThree release];
    [findVC release];
    [_tabFour release];
    
    
    
    self.openView = [[UIImageView alloc] initWithFrame:self.window.frame];
    self.openView.image = [UIImage imageNamed:@"buff1.png"];
    
    self.mainView = [[UIImageView alloc] initWithFrame:self.window.frame];
    self.mainView.image = [UIImage imageNamed:@"buff2.png"];
    
    
    self.view = [[UIView alloc] initWithFrame:self.window.frame];
    
    [self.view addSubview:self.openView];
    [self.view addSubview:self.mainView];
    [self.window addSubview:self.view];

    [self performSelector:@selector(openAction) withObject:self afterDelay:1];

    [_openView release];
    [_mainView release];
    [_view release];
    [_window release];
    
    
    return YES;
}



- (void)openAction
{
    CATransition *animation = [CATransition animation];
    animation.delegate = self;
    animation.duration = 0.7 ;  // 动画持续时间(秒)
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    animation.type = kCATransitionFade;//淡入淡出效果
    
    NSUInteger f = [[self.view subviews] indexOfObject:self.openView];
    NSUInteger z = [[self.view subviews] indexOfObject:self.mainView];
    [self.view exchangeSubviewAtIndex:z withSubviewAtIndex:f];
    
    [[self.view layer] addAnimation:animation forKey:@"animation"];
    
    [self performSelector:@selector(ToUpSide) withObject:nil afterDelay:2];//2秒后执行TheAnimation
}

- (void)ToUpSide {
    
    [self moveToUpSide];//向上拉界面
    
}

- (void)moveToUpSide {
    [UIView animateWithDuration:0.7 //速度0.7秒
                     animations:^{//修改rView坐标
                         self.view.frame = CGRectMake(self.window.frame.origin.x,
                                                       -self.window.frame.size.height,
                                                       self.window.frame.size.width,
                                                       self.window.frame.size.height);
                     }
                     completion:^(BOOL finished){
                     }];
    
}

- (void)tabChangeColor:(NSNotification *)notification
{
    if ([NightModel shareInstance].open == 1) {
        _tabBar.tabBar.backgroundImage = nil;
        _tabBar.tabBar.barTintColor = [UIColor blackColor];
        
        [_tabOne.navigationBar setBackgroundImage:[UIImage imageNamed:@"night360-60.png"] forBarMetrics:UIBarMetricsDefault];
        [_tabTow.navigationBar setBackgroundImage:[UIImage imageNamed:@"night360-60.png"] forBarMetrics:UIBarMetricsDefault];
        [_tabThree.navigationBar setBackgroundImage:[UIImage imageNamed:@"night360-60.png"] forBarMetrics:UIBarMetricsDefault];
        [_tabFour.navigationBar setBackgroundImage:[UIImage imageNamed:@"night360-60.png"] forBarMetrics:UIBarMetricsDefault];
    } else{
        _tabBar.tabBar.backgroundImage = [UIImage imageNamed:@"yu.png"];
        [_tabOne.navigationBar setBackgroundImage:[UIImage imageNamed:@"moren0.png"] forBarMetrics:UIBarMetricsDefault];
        [_tabTow.navigationBar setBackgroundImage:[UIImage imageNamed:@"moren1.png"] forBarMetrics:UIBarMetricsDefault];
        [_tabThree.navigationBar setBackgroundImage:[UIImage imageNamed:@"moren2.png"] forBarMetrics:UIBarMetricsDefault];
        [_tabFour.navigationBar setBackgroundImage:[UIImage imageNamed:@"moren3.png"] forBarMetrics:UIBarMetricsDefault];
    }
}


- (void)topicColor:(NSNotification *)niti
{
//    UIColor *color = niti.object;
//    _tabBar.tabBar.barTintColor = color;
//    
//    _tabFour.navigationBar.barTintColor = color;
//    _tabOne.navigationBar.barTintColor = color;
//    _tabTow.navigationBar.barTintColor = color;
//    _tabThree.navigationBar.barTintColor = color;
    
    NSString *string = niti.object;
    
    if ([string isEqualToString:@"moren0.png"]) {
         _tabBar.tabBar.backgroundImage = [UIImage imageNamed:@"yu.png"];
    } else if ([string isEqualToString:@"moren1.png"]) {
        _tabBar.tabBar.backgroundImage = nil;
        _tabBar.tabBar.barTintColor = [WebColor lemonChiffon];
    } else if ([string isEqualToString:@"moren2.png"]) {
        _tabBar.tabBar.backgroundImage = nil;
        _tabBar.tabBar.barTintColor = [WebColor lightGreen];
    } else if ([string isEqualToString:@"moren3.png"]) {
        _tabBar.tabBar.backgroundImage = nil;
        _tabBar.tabBar.barTintColor = [WebColor gainsboro];
    }
    
    [_tabOne.navigationBar setBackgroundImage:[UIImage imageNamed:string] forBarMetrics:UIBarMetricsDefault];
    [_tabTow.navigationBar setBackgroundImage:[UIImage imageNamed:string] forBarMetrics:UIBarMetricsDefault];
    [_tabThree.navigationBar setBackgroundImage:[UIImage imageNamed:string] forBarMetrics:UIBarMetricsDefault];
    [_tabFour.navigationBar setBackgroundImage:[UIImage imageNamed:string] forBarMetrics:UIBarMetricsDefault];
}

- (void)dealloc
{
    [_window release];
    [_openView release];
    [_mainView release];
    [_view release];
    [_tabBar release];
    [_tabFour release];
    [_tabOne release];
    [_tabThree release];
    [_tabTow release];
    [super dealloc];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
